require "json"
require "TaiShouFuScene"
require "YuanZhenScene"
--require "GameObject"
require "ActionUtil"
require "NodeManager"
require "MainScene"
require "GuanKaScene"
require "EquipHeroScene"

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(msg) .. "\n")
    print(debug.traceback())
    print("----------------------------------------")
end

local function main()
    -- avoid memory leak
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)

    local visibleSize = CCDirector:sharedDirector():getVisibleSize()
    local origin = CCDirector:sharedDirector():getVisibleOrigin()

    
    -- http
    --CCHttpClient:request("http://user.alonew.com/login/serverMap?group=2", "onHttpCompleted")
    -- run
    
    --mainScene = CCBReader:load("MainSceneHD.ccbi")
    --local sceneGame = CCScene:create()
    --sceneGame:addChild(MainScene.getNode())
    --setTouchEnabledAll(mainScene, false)
    --sceneGame:addChild(CCBReader:load("test.ccbi"))
    --sceneGame:addChild(YuanZhenScene.getNode())
    --sceneGame:addChild(GuanKaScene.getNode())
    --CCDirector:sharedDirector():runWithScene(MainScene.getScene())
    --CCDirector:sharedDirector():runWithScene(WarScene.getScene())
    CCDirector:sharedDirector():runWithScene(EquipHeroScene.getScene())

    --[[
    --local b=CCButton:createWithFileName("onTestButton","UI8.png","UI8.png","UI8.png")  
    local b=CCButton:createWithFileName("onTestButton","build1.png","build1.png","build1.png")  
    print("button", b)
    b:setPosition(200,200)    
    mainScene:addChild(b)    
    --b:setTouchEnabled(false)   
    ]]
    print("begin")
end

function onTestButton(node)
  print("onTestButton")
end

function onActionEnd(go, flag)
  if(flag=="attack") then
    print(go, flag)
    go:getCompant("ball"):setVisible(true)
    local mb = CCMoveBy:create(1, ccp(200,0))
    go:playActionEx("ball", mb, "onActionEnd:ball")
  elseif(flag=="ball") then
    go:getCompant("ball"):setPosition(0,0)
    go:getCompant("ball"):setVisible(false)
    local da = CCDelayTime:create(1)
    go:playActionEx("ball", da, "onActionEnd:delay")
  elseif(flag=="delay") then
    go:playAction("normal", "attack", "onActionEnd:attack");
  end
end

function onHttpCompleted(data)
  print("http")
  print(data)
  result = json.decode(data)
  for key, value in pairs(result) do  
    print(key)
    print(value)
    --print(string.format("%s --- %s", key, value))  
  end 

end

function onJunQi(node)
end

function onZhanDou(node)
  MainScene.enableTouch(false)
  local sc = CCDirector:sharedDirector():getRunningScene()
  sc:addChild(GuanKaScene.getNode())
  --GuanKaScene.node:setTag(1000)
end

function onGongDian(node)
  MainScene.enableTouch(false)
  local sc = CCDirector:sharedDirector():getRunningScene()
  local tsf = TaiShouFuScene:getNode()
  print(tsf)
  sc:addChild(tsf)
  return
end

function onClose(node)
  MainScene.enableTouch(true)
  --parent:setTouchEnabled(true)
  local sc = CCDirector:sharedDirector():getRunningScene()
  print("onClose", node)
  sc:removeChild(node:getParent(), true)
end

xpcall(main, __G__TRACKBACK__)
